/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package condivisa;

import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import connezione.PooledConnection;
import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.naming.NamingException;
import login.BeanUser;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 *
 * @author sestari
 */
public class BeamClientSms implements Serializable {

    PooledConnection conn;
    String xml = "";
    String numero = "";

    
    Boolean ceck = false;

    /** Creates a new instance of BeamClientSms */
    public BeamClientSms() {
        conn = (PooledConnection) ((BeanUser) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user")).getCon();
    }

    public Map<String, String> getCelulari() throws SQLException, NamingException {
        Map<String, String> myModelValues = new HashMap<String, String>();
        Statement stmt = (Statement) conn.createStatement();
        ResultSet result = (ResultSet) stmt.executeQuery("SELECT cellulare FROM Anagrafica order by cellulare");


        while (result.next()) {
            myModelValues.put(result.getString("cellulare"), result.getString("cellulare"));

        }
        return myModelValues;
    }

    public void sendSMS() throws MalformedURLException, ProtocolException, IOException {
        try {
            String cara = "F";
            if (getCeck()){
                cara = "V";
            }
            String urlcomando = "http://localhost:8080/xptoWebService/customers/";
            urlcomando += getNumero()+ "--" + cara ;
            URL url = new URL(urlcomando);

            URLConnection con = url.openConnection();
            ((HttpURLConnection) con).setRequestMethod("GET");

            con.setDoOutput(true);
            con.setConnectTimeout(20000);
            con.setUseCaches(false);
            con.setDefaultUseCaches(false);


            InputStreamReader reader = new InputStreamReader(con.getInputStream());
            StringBuilder buf = new StringBuilder();
            char[] cbuf = new char[2048];
            int num;
            while (-1 != (num = reader.read(cbuf))) {
                buf.append(cbuf, 0, num);
            }
            xml = buf.toString();
            
        } catch (Throwable t) {
           xml = t.getMessage();
        }
    }

    public String getXml() {
        return xml;
    }

    public void setXml(String xml) {
        this.xml = xml;
    }

    public String getNumero() {
        return numero;
    }

    public void setNumero(String numero) {
        this.numero = numero;
    }
    public Boolean getCeck() {
        return ceck;
    }

    public void setCeck(Boolean ceck) {
        this.ceck = ceck;
    }
}
